package com.example.vue_shop.components.rights.controller;

import com.example.vue_shop.components.rights.entity.Rights;
import com.example.vue_shop.components.rights.entity.TreeRights;
import com.example.vue_shop.components.rights.service.RightsService;
import com.example.vue_shop.utils.ResultN;
import com.example.vue_shop.utils.ResultObj;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author lipengwei
 */
@Api(tags = "权限管理")
@RestController
@RequestMapping("/rights")
public class RightsController {

    @Resource(name = "rightsServiceImpl")
    private RightsService rightsService;

    /**
     * 获取权限列表.
     *
     * @return List<Map<String, Object>>
     */
    @GetMapping("/getRightsList")
    @ApiOperation("获取权限列表")
    public String getRightsList() {
        try {
            Map<String, Object> rightsList = rightsService.getRightsList();
            return ResultN.success(rightsList, "获取权限列表成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultN.error("获取权限列表失败！");
        }
    }


    /**
     * 获取所有权限，树形结构含分页.
     * @param pageNum 页数 默认1
     * @param pageSize 每页显示的数量 默认7
     * @return String
     */
    @ApiOperation("获取所有的权限（树形结构含分页）")
    @GetMapping("/getAllRights/tree/page")
    public String getAllRightsTreePage (
            @RequestParam(required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "7") int pageSize) {
        try {
            Map<String, Object> allRightsTreePage = rightsService.getAllRightsTreePage(pageNum, pageSize);
            return ResultN.success(allRightsTreePage, "获取权限列表成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultN.error("获取权限列表失败");
        }
    }

    /**
     * 获取所有的权限列表（返回为树形结构）.
     * @return List<TreeRights>
     */
    @ApiOperation("获取所有的权限")
    @GetMapping("/getAllRights/tree")
    public ResultObj<List<TreeRights>> getAllRightsTree () {
        ResultObj<List<TreeRights>> resultObj = new ResultObj<>();
        try {
            List<TreeRights> rightsListAll = rightsService.getAllRightsTree();
            resultObj.setData(rightsListAll);
            resultObj.setMessage("获取权限列表成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setStatus(false);
            resultObj.setMessage("获取权限列表失败！");
        }
        return resultObj;
    }

    /**
     * 获取全部的权限（返回为list格式）.
     * @return List<Rights>
     */
    @ApiOperation("获取全部的权限")
    @GetMapping("/getAllRights/list")
    public ResultObj<List<Rights>> getAllRightsList() {
        ResultObj<List<Rights>> resultObj = new ResultObj<>();
        try {
            List<Rights> all = rightsService.getAllRightsList();
            resultObj.setData(all);
            resultObj.setMessage("请求所有权限返回成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setStatus(false);
            resultObj.setMessage("请求所有权限角色返回失败");
        }
        return resultObj;
    }


    /**
     * 新增权限.
     * @param rights 权限对象.
     * @return String
     */
    @PostMapping("/addRights")
    public String addRights(@RequestBody Rights rights) {
        try{
            rightsService.addRights(rights);
            return ResultN.success("新增权限成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultN.error("新增权限失败！");
        }

    }

    /**
     * 更新权限.
     * @param rights 权限对象
     * @return String
     */
    @PutMapping("/updateRights")
    public String updateRights(@RequestBody Rights rights) {
        try {
            rightsService.updateRights(rights);
            return ResultN.success("更新权限成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultN.error("更新权限失败！");
        }
    }

    /**
     * 删除权限.
     * @param id 权限Id
     * @return String
     */
    @DeleteMapping("/delRights")
    public String delRights(int id) {
        try{
            rightsService.delRights(id);
            return ResultN.success("删除权限成功！");
        }catch (Exception e) {
            e.printStackTrace();
            return ResultN.error("删除权限失败！");
        }
    }

}
